//
//  BSLGalleryPlugin.m
//  Demo
//
//  Created by RYAN on 15/6/17.
//
//

#import "BSLGalleryPlugin.h"
#import "BSLAlbumsTableViewController.h"
#import "AppDelegate.h"

@implementation GalleryNavigationController

- (void)dismiss:(NSMutableArray *)paths errorMessage:(NSString *)message{
    
    __weak GalleryNavigationController *weakSelf = self;
    [self dismissViewControllerAnimated:[BSLGalleryHelper shareInatance].isAnimated completion:^{

        CDVPluginResult *pluginResult;
        if (!paths || paths.count ==0) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:((message) ? message : @"error but no prompt")];
        }
        
        else{
            if (paths.count == 1){//单选
                NSString *path = [paths firstObject];
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:path];
            }
            
            else{
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:paths];
            }
        }
        
        [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:weakSelf.callbackID];
    }];
}

@end

@implementation BSLGalleryPlugin

- (void)pick:(CDVInvokedUrlCommand*)command{
    
    [self.commandDelegate runInBackground:^{
        
        NSString *callbackID = command.callbackId;
        NSDictionary *parms = command.arguments[0];
        BSLGalleryHelper *helper = [BSLGalleryHelper shareInatance];
        [helper setPropertyWithDic:parms];
        
        BSLAlbumsTableViewController *bslAlbumsTableViewController = [[BSLAlbumsTableViewController alloc] initWithStyle:UITableViewStylePlain];
        AppDelegate *appDelegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
        
        GalleryNavigationController *navC = [[GalleryNavigationController alloc] initWithRootViewController:bslAlbumsTableViewController];
        navC.callbackID = callbackID;
        navC.commandDelegate = self.commandDelegate;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [appDelegate.window.rootViewController presentViewController:navC animated:helper.isAnimated completion:^{
                
            }];

        });
    }];
}

- (void)save:(CDVInvokedUrlCommand*)command{
    
    [self.commandDelegate runInBackground:^{
        
        self.callbackID = command.callbackId;
        NSString *path = command.arguments[0];
        UIImage *img = [UIImage imageWithContentsOfFile:path];
        UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(NSString *)contextInfo{
    
    CDVPluginResult *result = nil;
    if (error == nil) {
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    }
    
    else{
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:(int)error.code];
    }
    
    [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
}

@end
